// 经典面试题：

export const _updateVersion = (
  oldVersion: string,
  newVersion: string
): number => {
  // newVersion > oldVersion    =>  1
  // newVersion < oldVersion    =>  -1
  // newVersion =  oldVersion   =>  0
  const oldArr = oldVersion.split(".");
  const newArr = newVersion.split(".");

  const len = Math.max(oldArr.length, newArr.length);

  for (let i = 0; i < len; i++) {
    const old = oldArr[i] ? oldArr[i] : 0;
    const news = newArr[i] ? newArr[i] : 0;
    if (news > old) return 1;
    else if (news < old) return -1;
  }

  return 0;
};

// 测试用例
// console.log(_updateVersion("1.2.1", "1.2.3")); // => 1
// console.log(_updateVersion("1.22.1", "1.2.3")); // => -1
// console.log(_updateVersion("1.2", "1.2.0")); // => 0
// console.log(_updateVersion("1.3.1", "1.2.3")); // => -1
// console.log(_updateVersion("1.3.0.0.0.0.0.0", "1.3.1")); // => 1
